﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace Meraz.Core.Model
{
    [DataContract]
    [Serializable]
    public abstract class Study 
    {
        const string DATE_FORMAT = "MM/d/yyyy";
        static Regex DATE_REGEX = new Regex(@"0?(?<MONTH>\d+)[/-]0?(?<DAY>\d+)[/-](?<YEAR>\d+)");
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Name { get; set; }

        public DateTime StartDate { get; set; }

        public long StartTicks
        {
            get { return StartDate.Ticks; }
            set { StartDate = new DateTime(value); }
        }

        [XmlIgnore]
        [DataMember]
        public string Start
        {
            get {
                return StartDate.ToShortDateString(); 
            }
            set
            {
                StartDate = ParseDateString(value);
            }
        }

        private DateTime ParseDateString(string dateString)
        {
            DateTime dt = DateTime.MinValue;
            Match match = DATE_REGEX.Match(dateString);
            if (match.Success)
            {
                int month = int.Parse(match.Groups["MONTH"].Value);
                int day = int.Parse(match.Groups["DAY"].Value);
                int year = int.Parse(match.Groups["YEAR"].Value);
                dt = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Utc);
            }
            return dt;
        }

        public DateTime EndDate { get; set; }

        public long EndTicks
        {
            get { return EndDate.Ticks; }
            set { EndDate = new DateTime(value); }
        }

        [XmlIgnore]
        [DataMember]
        public string End
        {
            get
            {
                return EndDate.ToShortDateString();
            }
            set
            {
                EndDate = ParseDateString(value);
            }
        }

        public string Notes { get; set; }

        public bool? IsTwitter { get; set; }
        public bool? IsBlog { get; set; }

        [DataMember]
        public string Status
        {
            get { return this.StatusType.ToString(); }
            set { this.StatusType = (StatusType)Enum.Parse(typeof(StatusType), value); }
        }
        [XmlIgnore]
        public StatusType StatusType { get; set; }

        public override string ToString()
        {
            string format = @"{0}: '{1}'";
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.AppendFormat(format, "Type", this.GetType());
            sb.Append(", ");
            sb.AppendFormat(format, "ID", ID);
            sb.Append(", ");
            sb.AppendFormat(format, "Name", Name);
            sb.Append(", ");
            sb.AppendFormat(format, "StartDate", StartDate);
            sb.Append(", ");
            sb.AppendFormat(format, "EndDate", EndDate);
            sb.Append(", ");
            sb.AppendFormat(format, "Status", Status);
            sb.Append("}");
            return sb.ToString();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (obj is Study)
                return Equals((Study)obj);
            return false;
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        #region IEquatable<Study> Members

        public bool Equals(Study other)
        {
            if (other == null)
                return false;

            return ID.Equals(other.ID);
        }

        #endregion
    }
}
